package com.jeeplus.modules.work.queue.web;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.FileUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.time.DateUtil;
import com.jeeplus.modules.sys.utils.FileKit;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.work.queue.entity.ElectronicMaterialEntity;
import com.jeeplus.modules.work.queue.service.ElectronicMaterialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "电子材料")
@RequiredArgsConstructor
@RequestMapping(value = "/ElectronicMaterial")
public class ElectronicMaterialController {
    private static final Logger log = LoggerFactory.getLogger(ElectronicMaterialController.class);
    private final ElectronicMaterialService electronicMaterialService;
    @PostMapping ("upElectronicMaterial")
    @ApiOperation(value = "上传材料")
    public AjaxJson  upElectronicMaterial(@RequestBody ElectronicMaterialEntity entity){
        log.error(UserUtils.getUser().toString());
        if(StringUtils.isNotBlank(entity.getIsCheck())){
            if(StringUtils.isBlank(entity.getElectronicMaterialsName()) || entity.getElectronicMaterialsName().length() <= 2){
                return AjaxJson.error("材料名称过短！");
            }else  if(electronicMaterialService.findMaterialNameCheck(entity.getElectronicMaterialsName()) >= 1){
                return AjaxJson.error("材料名称敏感！");
            }else  if(electronicMaterialService.findMaterialNameCount(entity.getElectronicMaterialsName(),UserUtils.getUser().getLoginName()) >= 1){
                return AjaxJson.error("材料名称已存在！");
            }
        }
        entity.setStatus("0");
        entity.setCreateTime(DateUtils.getDateTime());
        String substring = entity.getMaterialsUrl().substring(entity.getMaterialsUrl().lastIndexOf(".")+1,entity.getMaterialsUrl().length());
        entity.setMaterialsType(substring);
        if(null == UserUtils.getUser())return AjaxJson.error("用户失效");
        entity.setLoginId(UserUtils.getUser().getLoginName());
        if(StringUtils.isBlank(entity.getOid()))entity.setOid("暂无数据");
        if(StringUtils.isBlank(entity.getMaterialId()))entity.setMaterialId("暂无数据");
        Integer integer = electronicMaterialService.upElectronicMaterial(entity);
        if(integer>=1){
            return AjaxJson.success("操作成功！");
        }
        return AjaxJson.error("操作失败！");

    }
    @GetMapping ("getElectronicMaterial")
    @ApiOperation(value = "获取材料")
    public AjaxJson  getElectronicMaterial(String loginName){
        if(StringUtils.isNotBlank(loginName)){
            return AjaxJson.success().put("data",electronicMaterialService.getElectronicMaterial(loginName));
        } else if(UserUtils.getUser() != null && StringUtils.isNotBlank(UserUtils.getUser().getLoginName())){
            return AjaxJson.success().put("data",electronicMaterialService.getElectronicMaterial(UserUtils.getUser().getLoginName()));
        }
        return AjaxJson.error("登录失效!");
    }

    @GetMapping ("delElectronicMaterial")
    @ApiOperation(value = "删除材料")
    public AjaxJson  delElectronicMaterial(String id,String electronicMaterialsName){
        if(electronicMaterialService.delElectronicMaterial(id) >= 1){
            if(electronicMaterialsName.contains("身份证反面") || electronicMaterialsName.contains("身份证正面")){
                electronicMaterialService.delUserIdcardMaterial(UserUtils.getUser().getLoginName());
            }
            return AjaxJson.success("删除文件成功");
        }else{
            return AjaxJson.success("删除文件失败");
        }
    }

    public AjaxJson  setElectronicMaterial(String id,String oid,String materialId ,String electronicMaterialsName,String materialsUrl,String materialsType,String loginId,String sort,String materialsItem){
        ElectronicMaterialEntity entity=new ElectronicMaterialEntity();
        entity.setId(id);
        entity.setOid(oid);
        entity.setMaterialId(materialId);
        entity.setElectronicMaterialsName(electronicMaterialsName);
        entity.setMaterialsUrl(materialsUrl);
        entity.setMaterialsType(materialsType);
        entity.setLoginId(loginId);
        entity.setSort(sort);
        entity.setMaterialsItem(materialsItem);
        Integer i = electronicMaterialService.setElectronicMaterial(entity);
        if(i>=1){
            return AjaxJson.success("修改成功！");
        }
        return AjaxJson.success("修改失败！");
    }

    @PostMapping ("upElectronicOldMaterial")
    @ApiOperation(value = "上传材料")
    public AjaxJson  upElectronicOldMaterial(@RequestBody ElectronicMaterialEntity entity){
        entity.setStatus("0");
        entity.setCreateTime(DateUtils.getDateTime());
        String substring = entity.getMaterialsUrl().substring(entity.getMaterialsUrl().lastIndexOf(".")+1,entity.getMaterialsUrl().length());
        entity.setMaterialsType(substring);
        if(null == UserUtils.getUser())return AjaxJson.error("用户失效");
        entity.setLoginId(UserUtils.getUser().getLoginName());
        Integer integer = electronicMaterialService.upElectronicOldMaterial(entity);
        if(integer>=1){
            if(entity.getElectronicMaterialsName().contains("身份证反面") || entity.getElectronicMaterialsName().contains("身份证正面")){
                electronicMaterialService.delUserIdcardMaterial(UserUtils.getUser().getLoginName());
            }
            return AjaxJson.success("操作成功！");
        }
        return AjaxJson.error("操作失败！");

    }

    //材料授权
    @GetMapping ("updateMaterialStatus")
    public AjaxJson updateMaterialStatus(String id,int status,String electronicMaterialsName,String doName){
        if(electronicMaterialService.updateMaterialStatus(id,status) >= 1){
            electronicMaterialService.intoYmbcsRemark(id,"于"+DateUtils.getDateTime()+"对"+electronicMaterialsName+"材料进行"+doName,UserUtils.getUser().getLoginName());
            if((electronicMaterialsName.contains("身份证反面") || electronicMaterialsName.contains("身份证正面")) && status == 2){
                electronicMaterialService.delUserIdcardMaterial(UserUtils.getUser().getLoginName());
            }
            return AjaxJson.success("操作成功");
        }else{
            return AjaxJson.success("操作失败");
        }
    }

    //材料授权记录查询
    @GetMapping ("findYmbcsRemark")
    public AjaxJson findYmbcsRemark(){
        List<String> ymbcsRemark = electronicMaterialService.findYmbcsRemark(UserUtils.getUser().getLoginName());
        if(null == ymbcsRemark || ymbcsRemark.size() == 0)ymbcsRemark.add("该用户暂无任何授权操作记录。");
        return AjaxJson.success().put("data",ymbcsRemark);
    }

}
